

package kr.ac.ssu.yoobh17.mPlayer.customView;


import kr.ac.ssu.yoobh17.mPlayer.R;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.FrameLayout;


public class SuffleButton extends FrameLayout implements OnTouchListener {

	private Button	button_suffle;
	private boolean	isSuffle	= false;

	//---------------------------------------------------------------------------------------------------

	private OnSuffleButtonClickListner suffleButtonClickListner = null;
	
	public static interface OnSuffleButtonClickListner{
		
		public void onSuffleButtonClick( boolean isSuffle );
		
	}
	
	public void setOnSuffleButtonClickListner( OnSuffleButtonClickListner suffleButtonClickListner ){

		this.suffleButtonClickListner = (OnSuffleButtonClickListner) suffleButtonClickListner;

	}

	//---------------------------------------------------------------------------------------------------
	
	public SuffleButton( Context context ) {

		this( context, null );
		// TODO Auto-generated constructor stub
	}

	public SuffleButton( Context context, AttributeSet attrs ) {

		super( context, attrs );
		// TODO Auto-generated constructor stub

		initView( context );

	}

	private void initView( Context context ) {

		// TODO Auto-generated method stub

		LayoutParams layoutParams = new LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT );

		button_suffle = new Button( context );
		button_suffle.setLayoutParams( layoutParams );
		button_suffle.setOnTouchListener( this );
		
		addView( button_suffle );

		isSuffle = false;
		button_suffle.setBackgroundResource( R.drawable.image_suffle_false );

	}

	//---------------------------------------------------------------------------------------------------
	
	@Override
	public boolean onTouch( View view, MotionEvent event ) {

		// TODO Auto-generated method stub
		
		switch ( event.getAction() ) {
		
		case MotionEvent.ACTION_DOWN:
			
			if( isSuffle ){

				button_suffle.setBackgroundResource( R.drawable.image_suffle_true_pressed );
				
			}
			else{

				button_suffle.setBackgroundResource( R.drawable.image_suffle_false_pressed );
				
			}

			return true;

		case MotionEvent.ACTION_UP:

			toggleButton();
			
			if( null != suffleButtonClickListner ){
				
				suffleButtonClickListner.onSuffleButtonClick( isSuffle );
				
			}

			return true;

		}
		
		return false;
	}

	private void toggleButton() {

		if ( isSuffle ) {

			button_suffle.setBackgroundResource( R.drawable.image_suffle_false );

			isSuffle = false;

		}
		else {

			button_suffle.setBackgroundResource( R.drawable.image_suffle_true );

			isSuffle = true;

		}

	}

	//---------------------------------------------------------------------------------------------------
	
	public void setState( boolean isSuffle ) {

		this.isSuffle = isSuffle;

		if ( isSuffle ) {

			button_suffle.setBackgroundResource( R.drawable.image_suffle_true );

		}
		else {

			button_suffle.setBackgroundResource( R.drawable.image_suffle_false );

		}

	}

	public boolean getState() {

		return isSuffle;
	}

}
